<?php

namespace backend\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Order;

/**
 * OrderSearch represents the model behind the search form of `app\models\Order`.
 */
class OrderSearch extends Order
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'magento_order_id', 'storehouse_id', 'payment_at', 'process_at', 'has_shipment', 'shipped_at', 'shipped_count', 'total_item_count', 'edited', 'approved', 'blocked', 'paused', 'paused_uid', 'changed', 'reshipped', 'expedited', 'created_at', 'updated_at', 'uid', 'website_id', 'store_id', 'package_return', 'ups_special', 'has_gift', 'has_prime_gift', 'presale', 'magento_return_id', 'has_print_shipment_label', 'has_bundle', 'customer_id', 'haiwaicang_export', 'haiwaicang_export_uid', 'is_fraud', 'distributor_order_id', 'blocked2', 'weight', 'sync_success'], 'integer'],
            [['ext_order_id', 'increment_id', 'payment_method', 'payment_status', 'order_type', 'order_status', 'last_track_status', 'shipping_method', 'shipping_method_origin', 'shipping_description', 'shipping_description_origin', 'shipping_track_no', 'waybill', 'from', 'currency_code', 'discount_description', 'transaction_id', 'presale_timeline', 'coupon_code', 'coupon_rule_name', 'google_order_id', 'magento_user_lev_ordered', 'customer_group', 'guuid', 'distributor', 'source', 'email', 'orders_type'], 'safe'],
            [['grand_total', 'base_grand_total', 'static_grand_total', 'subtotal', 'base_subtotal', 'base_discount_amount', 'base_shipping_amount', 'shipping_amount', 'discount_amount', 'discount_points', 'discount_points_amount', 'base_discount_points_amount', 'system_discount', 'shipping_insurance', 'shipping_discount_amount', 'base_shipping_discount_amount', 'store_credit_amount', 'static_store_credit_amount', 'base_store_credit_amount', 'gift_card_discount_amount', 'base_gift_card_discount_amount', 'tariff_insurance_amount', 'base_tariff_insurance_amount', 'prime_discount_amount', 'base_prime_discount_amount', 'shipping_tax_amount'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Order::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'magento_order_id' => $this->magento_order_id,
            'storehouse_id' => $this->storehouse_id,
            'payment_at' => $this->payment_at,
            'process_at' => $this->process_at,
            'has_shipment' => $this->has_shipment,
            'shipped_at' => $this->shipped_at,
            'shipped_count' => $this->shipped_count,
            'total_item_count' => $this->total_item_count,
            'edited' => $this->edited,
            'approved' => $this->approved,
            'blocked' => $this->blocked,
            'paused' => $this->paused,
            'paused_uid' => $this->paused_uid,
            'changed' => $this->changed,
            'reshipped' => $this->reshipped,
            'expedited' => $this->expedited,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'uid' => $this->uid,
            'website_id' => $this->website_id,
            'store_id' => $this->store_id,
            'grand_total' => $this->grand_total,
            'base_grand_total' => $this->base_grand_total,
            'static_grand_total' => $this->static_grand_total,
            'subtotal' => $this->subtotal,
            'base_subtotal' => $this->base_subtotal,
            'base_discount_amount' => $this->base_discount_amount,
            'base_shipping_amount' => $this->base_shipping_amount,
            'shipping_amount' => $this->shipping_amount,
            'discount_amount' => $this->discount_amount,
            'discount_points' => $this->discount_points,
            'discount_points_amount' => $this->discount_points_amount,
            'base_discount_points_amount' => $this->base_discount_points_amount,
            'system_discount' => $this->system_discount,
            'package_return' => $this->package_return,
            'shipping_insurance' => $this->shipping_insurance,
            'shipping_discount_amount' => $this->shipping_discount_amount,
            'base_shipping_discount_amount' => $this->base_shipping_discount_amount,
            'store_credit_amount' => $this->store_credit_amount,
            'static_store_credit_amount' => $this->static_store_credit_amount,
            'base_store_credit_amount' => $this->base_store_credit_amount,
            'gift_card_discount_amount' => $this->gift_card_discount_amount,
            'base_gift_card_discount_amount' => $this->base_gift_card_discount_amount,
            'ups_special' => $this->ups_special,
            'has_gift' => $this->has_gift,
            'has_prime_gift' => $this->has_prime_gift,
            'presale' => $this->presale,
            'magento_return_id' => $this->magento_return_id,
            'tariff_insurance_amount' => $this->tariff_insurance_amount,
            'base_tariff_insurance_amount' => $this->base_tariff_insurance_amount,
            'prime_discount_amount' => $this->prime_discount_amount,
            'base_prime_discount_amount' => $this->base_prime_discount_amount,
            'shipping_tax_amount' => $this->shipping_tax_amount,
            'has_print_shipment_label' => $this->has_print_shipment_label,
            'has_bundle' => $this->has_bundle,
            'customer_id' => $this->customer_id,
            'haiwaicang_export' => $this->haiwaicang_export,
            'haiwaicang_export_uid' => $this->haiwaicang_export_uid,
            'is_fraud' => $this->is_fraud,
            'distributor_order_id' => $this->distributor_order_id,
            'blocked2' => $this->blocked2,
            'weight' => $this->weight,
            'sync_success' => $this->sync_success,
        ]);

        $query->andFilterWhere(['like', 'ext_order_id', $this->ext_order_id])
            ->andFilterWhere(['like', 'increment_id', $this->increment_id])
            ->andFilterWhere(['like', 'payment_method', $this->payment_method])
            ->andFilterWhere(['like', 'payment_status', $this->payment_status])
            ->andFilterWhere(['like', 'order_type', $this->order_type])
            ->andFilterWhere(['like', 'order_status', $this->order_status])
            ->andFilterWhere(['like', 'last_track_status', $this->last_track_status])
            ->andFilterWhere(['like', 'shipping_method', $this->shipping_method])
            ->andFilterWhere(['like', 'shipping_method_origin', $this->shipping_method_origin])
            ->andFilterWhere(['like', 'shipping_description', $this->shipping_description])
            ->andFilterWhere(['like', 'shipping_description_origin', $this->shipping_description_origin])
            ->andFilterWhere(['like', 'shipping_track_no', $this->shipping_track_no])
            ->andFilterWhere(['like', 'waybill', $this->waybill])
            ->andFilterWhere(['like', 'from', $this->from])
            ->andFilterWhere(['like', 'currency_code', $this->currency_code])
            ->andFilterWhere(['like', 'discount_description', $this->discount_description])
            ->andFilterWhere(['like', 'transaction_id', $this->transaction_id])
            ->andFilterWhere(['like', 'presale_timeline', $this->presale_timeline])
            ->andFilterWhere(['like', 'coupon_code', $this->coupon_code])
            ->andFilterWhere(['like', 'coupon_rule_name', $this->coupon_rule_name])
            ->andFilterWhere(['like', 'google_order_id', $this->google_order_id])
            ->andFilterWhere(['like', 'magento_user_lev_ordered', $this->magento_user_lev_ordered])
            ->andFilterWhere(['like', 'customer_group', $this->customer_group])
            ->andFilterWhere(['like', 'guuid', $this->guuid])
            ->andFilterWhere(['like', 'distributor', $this->distributor])
            ->andFilterWhere(['like', 'source', $this->source])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'orders_type', $this->orders_type]);

        return $dataProvider;
    }
}
